<?php

namespace Api\Controller;

/**
 * 文化荟萃
 *
 * Class EventController
 * @package Api\Controller
 */
class CultureController extends PublicController
{
    private $cultural_model;
    private $image_model;


    public function _initialize()
    {
        parent::_initialize();
        $this->cultural_model = M('cultural_blend');
        $this->image_model = M('blend_image');
    }

    /**
     * 获取文化荟萃分页数据
     */
    public function getList()
    {
        $perPage = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 10;
        $curPage = !empty($_GET['page']) ? intval($_GET['page']) : 1;
        $where = "is_del=0 AND review=1";
        $count = $this->cultural_model->where($where)->count();
        $totalPage = ceil($count / $perPage);
        $list = $this->cultural_model->where($where)->field('id,title,author')->order('sort = 0, sort asc, id desc')->limit(($curPage - 1) * $perPage, $perPage)->select();
        foreach ($list as $k => $v) {
            $getImage = $this->image_model->where('c_id=' . $v['id'])->order('sort asc')->find();

            $list[$k]['cover'] = __DATAURL__ . $getImage['cover'];
        }

        $this->json_result([
            'per_page'   => $perPage,
            'total'      => $count,
            'total_page' => $totalPage,
            'cur_page'   => $curPage,
            'list'       => $list,
        ]);
    }

    /**
     * 获取文化详情
     */
    public function detail()
    {
        $id = intval($_REQUEST['id']);
        if (empty($id)) {
            $this->json_error('参数错误');
        }
        $detail = $this->cultural_model->where(['id' => $id])->find();
        if (!$detail) {
            $this->json_error('没有找到相关信息');
        }

        $images = $this->image_model->where(['c_id' => $id])->order("sort asc")->select();

        foreach ($images as $k => $v) {
            $detail['images'][$k] = $v;
            $detail['images'][$k]['cover'] = __DATAURL__ . $v['cover'];
        }

        $this->json_result($detail);
    }

    /**
     * 添加作品
     */
    public function addCulture()
    {
        $postData = file_get_contents('php://input');
        $postData = json_decode($postData, true);
        $this->t_session = $postData['t_session'];

        $userId = intval($this->get("id"));
        if (!$userId) {
            $this->json_error("请登录");
        }

        if (empty($postData['title']) || empty($postData['author'])) {
            $this->json_error("参数错误");
        }

        $data = [
            'u_id'      => $userId,
            'title'     => $postData['title'],
            'author'    => $postData['author'],
            'review'    => 0,
            'sort'      => 0,
            'create_at' => date('Y-m-d H:i:s'),
        ];
        $result_id = $this->cultural_model->add($data);
        $time = date('Y-m-d H:i:s');

        foreach ($postData['imgArr'] as $k => $v) {
            $sql = " INSERT INTO lr_blend_image(c_id,cover,intro,sort,create_at) VALUES ($result_id,'" . $v['cover'] . "','" . $v['intro'] . "','" . $v['sort'] . "','$time')";
            $this->cultural_model->execute($sql);
        }
        if ($result_id) {
            $this->json_result([], '操作成功');
        } else {
            $this->json_error("操作失败");
        }
    }

    /**
     * 我的作品
     */
    public function myZuoPin()
    {
        $userId = intval($this->get("id"));
        if (!$userId) {
            $this->json_error("请登录");
        }

        $perPage = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 10;
        $curPage = !empty($_GET['page']) ? intval($_GET['page']) : 1;
        $where = "is_del=0 AND u_id=" . $userId;

        $count = $this->cultural_model->where($where)->count();
        $totalPage = ceil($count / $perPage);
        $list = $this->cultural_model->where($where)->order('sort desc')->limit(($curPage - 1) * $perPage, $perPage)->select();
        foreach ($list as $k => $v) {
            $getImage = $this->image_model->where('c_id=' . $v['id'])->order('sort asc')->find();
            $list[$k]['cover'] = __DATAURL__ . $getImage['cover'];
        }

        $this->json_result([
            'per_page'   => $perPage,
            'total'      => $count,
            'total_page' => $totalPage,
            'cur_page'   => $curPage,
            'list'       => $list,
        ]);

    }
}